package com.ym.soso.service.impl;

import com.ym.common.utils.StringUtil;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.service.ISysCodeRuleSV;
import com.ym.soso.dao.SveNurseComeDao;
import com.ym.soso.domain.entity.SveNurseComeEntity;
import com.ym.soso.domain.param.AppSveNurseComeQueryParam;
import com.ym.soso.domain.param.PublishParam;
import com.ym.soso.domain.param.SveNurseComeQueryParam;
import com.ym.soso.domain.param.SveNurseComeUpdateParam;
import com.ym.soso.domain.vo.AppSveNurseComeVO;
import com.ym.soso.domain.vo.SveNurseComeListPcVO;
import com.ym.soso.service.ISveNurseComeSV;
import com.ym.soso.service.search.service.SveNurseComeIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 护士上门Service业务层处理
 *
 * @author ym
 * @date 2020-09-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SveNurseComeSVImpl extends BaseSVImpl<SveNurseComeEntity, SveNurseComeDao> implements ISveNurseComeSV {

	@Autowired
	private SveNurseComeIndexService sveNurseComeIndexService;

	@Autowired
	private ISysCodeRuleSV sysCodeRuleSV;

	/**
	 * 查询护士上门列表
	 *
	 * @param queryParam 护士上门 查询参数
	 * @return 护士上门
	 */
	@Override
	public List<SveNurseComeListPcVO> selectListPC(SveNurseComeQueryParam queryParam) {
		List<SveNurseComeListPcVO> sveNurseComes = mapper.selectListPC(queryParam);
		return sveNurseComes;
	}

	/**
	 * 新增护士上门
	 *
	 * @param updateParam 护士上门 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(SveNurseComeUpdateParam updateParam) {
		SveNurseComeEntity entity = updateParam.covert();
		String code = sysCodeRuleSV.createCode(CodeRuleEnum.NURSE_DOOR_TO_DOOR);
		entity.setTheCode(code);
		entity.setCreSb(ShiroUtil.getLoginId());
		this.insert(entity);
		searchUpdateIndex(entity.getId());
		return entity.getId();
	}

	/**
	 * 修改护士上门
	 *
	 * @param updateParam 护士上门 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(SveNurseComeUpdateParam updateParam) {
		SveNurseComeEntity entity = updateParam.covert();
		entity.setUptSb(ShiroUtil.getLoginId());
		int result = this.updateById(entity);
		searchUpdateIndex(entity.getId());
		return result;
	}

	@Override
	public List<AppSveNurseComeVO> selectMobileList(AppSveNurseComeQueryParam queryParam) {
		List<Long> ids = sveNurseComeIndexService.searchByKeywordOfIds(queryParam);
		if (StringUtil.isNotBlank(queryParam.getKeyword()) && ids.isEmpty()) {
			return Collections.emptyList();
		}
		if (!ids.isEmpty()) {
			queryParam.setIds(ids.toArray(new Long[]{}));
		}
		if (StringUtil.isNotBlank(queryParam.getKeyword())) {
			queryParam.setKeywordSort(true);
		}
		return mapper.selectMobileList(queryParam);
	}

	@Override
	public void publish(PublishParam params) {
		this.updateByIds(
				SveNurseComeEntity.builder()
						.publishStatus(params.getState())
						.publishTime(new Date())
						.publisher(ShiroUtil.getLoginId())
						.build(),
				params.getIds()
		);
		Long[] ids = params.getIds();
		List<SveNurseComeEntity> sveNurseComeEntities = this.selectList(SelectWhere().andIn(SveNurseComeEntity::getId, ids).end());
		sveNurseComeIndexService.saveOrUpdate(sveNurseComeEntities);
	}


	/**
	 * 查询数据并更新
	 */
	private void searchUpdateIndex(Long id) {
		SveNurseComeEntity entity = this.selectById(id);
		if (entity != null) {
			sveNurseComeIndexService.saveOrUpdate(entity);
		}
	}
}
